package org.surelink.radius.console;

import org.surelink.radius.cache.GroupCache;
import org.surelink.radius.console.message.Request;
import org.surelink.radius.console.message.RespBase;
import org.surelink.radius.console.message.Response;
import org.surelink.radius.constant.GroupStatus;
import org.surelink.radius.model.Group;
import de.tototec.cmdoption.CmdOption;
import de.tototec.cmdoption.CmdlineParser;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.List;
import java.util.Set;

public class GroupProcess extends BaseProcess
{

    @Autowired
    private GroupCache groupCache;

    class Config
    {
        @CmdOption(names = {"--help"}, description = "打印帮助", isHelp = true)
        private boolean help;

        @CmdOption(names = {"set"}, description = "设置用户组")
        private boolean set;

        @CmdOption(names = {"get"}, description = "查询用户组")
        private boolean get;

        @CmdOption(names = {"list"}, description = "查询用户组列表")
        private boolean list;

        @CmdOption(names = {"group"}, description = "用户组命令", hidden = true)
        boolean setgroup;

        @CmdOption(names = {"--group", "-g"}, args = {"groupname"}, minCount = -1, description = "组名")
        String groupname;

        @CmdOption(names = {"--status", "-s"}, args = {"status"}, minCount = -1, description = "组状态:0:停机/1:正常")
        String status;
    }


    public GroupProcess()
    {
        setOperate("group");
    }

    @Override
    public Response process(Request req) throws IOException
    {
        Config config = new Config();
        CmdlineParser cp = new CmdlineParser(config);
        cp.setProgramName(getOperate());
        cp.parse(req.getCommand().split("\\s+"));

        if (config.help)
        {
            StringBuilder sb = new StringBuilder();
            cp.usage(sb);
            return new Response(RespBase.SUCCESS.getCode(), sb.toString());
        }

        if (config.set)
        {
            if (config.groupname == null)
                return new Response(RespBase.MSG_FORMAT_ERR.getCode(), "用户组名不能为空");

            Group group = new Group();
            group.setGroupName(config.groupname);
            group.setStatus(config.status != null ? Integer.parseInt(config.status) : GroupStatus.Normal.value());
            group.setGroupDesc(config.groupname);
            groupCache.set(group);

            return RespBase.SUCCESS;
        }
        else if (config.get)
        {
            if (config.groupname == null)
                return new Response(RespBase.MSG_FORMAT_ERR.getCode(), "用户组名不能为空");

            Group group = groupCache.get(config.groupname);
            if (group == null)
                return new Response(RespBase.NOT_EXIST_ERR.getCode(), "用户组不存在");

            return new Response(group);
        }
        else if (config.list)
        {
            List<Group> groups = groupCache.list();
            if (groups == null)
                return new Response(RespBase.NOT_EXIST_ERR.getCode(), "用户组列表为空");

            return new Response(groups);
        }

        return RespBase.MSG_FORMAT_ERR;
    }
}
